
//避免全局变量的污染
const utils = (function () {
    const getStyle = (obj, attr) => {
        if (window.getComputedStyle) {
            return window.getComputedStyle(obj, null)[attr];
        }
        return obj.currentStyle[attr];
    }

    const animate = (oEle, json, callback) => {
        clearInterval(oEle.timer);
        oEle.timer = setInterval(() => {
            let flag = true;
            let target = 0;
            let current = 0;
            for (let attr in json) {
                if (attr === "opacity") {
                    target = parseFloat(json[attr]) * 100;
                    current = parseFloat(getStyle(oEle, attr)) * 100
                } else {
                    target = parseInt(json[attr])
                    current = parseInt(getStyle(oEle, attr))
                }
                let steps = (target - current) / 10;
                steps = steps > 0 ? Math.ceil(steps) : Math.floor(steps);

                if (attr === "opacity") {
                    oEle.style[attr] = (current + steps) / 100
                } else if (attr === "zIndex") {
                    oEle.style[attr] = target;
                } else {
                    oEle.style[attr] = (current + steps) + "px"
                }
                if (current != target) {
                    flag = false;
                }
            }
            if (flag) {
                clearInterval(oEle.timer);
                if (typeof (callback) === "function") {
                    callback();
                }
            }
        }, 20)
    }

    const getScroll = () => {
        //ie9+
        if (window.pageYOffset != undefined) {
            return {
                left: window.pageXOffset,
                top: window.pageYOffset
            }
        } else if (document.compatMode === 'CSS1Compat') {
            //带DTD
            return {
                left: document.documentElement.scrollLeft,
                top: document.documentElement.scrollTop
            }
        }
        //不带DTD
        return {
            left: document.body.scrollLeft,
            top: document.body.scrollTop
        }
    }

    return {
        getStyle,
        animate,
        getScroll
    }
})()

